#include "libs/path.h"
#include "kernel/log/log.h"


Path::Path(char* path/* args */)
{
    pdir = NULL;
    pfile = NULL;
    data = new String(path);

}

Path::~Path()
{
    if(pdir)
        delete pdir;

    if(pfile)
        delete pfile;
    if(pfileName)
        delete pfileName;
    if(pfileExeName)
        delete pfileExeName;

    delete data;
}



char* Path::dir()
{
    uint64_t index = data->count;
    char* buffer = data->charBuffer();
    while(buffer[--index] != '/' && index >= 0);
    
    if(index>=0)
    {
        pdir = new char[index + 1];
        memset(pdir,0,index + 1);
        memcpy(buffer,pdir,index);
    }

    return pdir;
}

char* Path::file()
{   
   
    int64_t index = data->count;
    char* buffer = data->charBuffer();
    while(index >= 0 && buffer[--index] != '/');
   
    if(index>=0)
    {
        pfile = new char[data->count - index];
        
        memset(pfile,0,data->count - index);
        memcpy(buffer + index + 1,pfile,data->count - index - 1);
    }
   
    return pfile;
}

char* Path::fileName()
{
    char* buffer = this->file();
	
    uint64_t count = strlen(buffer);
    uint64_t index = count;
    while(buffer[--index] != '.' && index >= 0);
    if(index >= 0)
    {
        pfileName = new char[index + 1];
        memset(pfileName,0,index  + 1);
        memcpy(buffer,pfileName,index);
    }
    return pfileName;
}

char* Path::fileExeName()
{   
    char* buffer = this->file();
    uint64_t count = strlen(buffer);
    uint64_t index = count;
    while(buffer[--index] != '.' && index >= 0);
    if(index >= 0)
    {
        pfileExeName = new char[count - index];
        memset(pfileExeName,0,count-index);
        memcpy(buffer + index + 1,pfileExeName,count-index-1);
    }
    return pfileExeName;
}

